package com.isp.org.server.domain.service;

import com.alicp.jetcache.anno.CacheInvalidate;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isp.org.server.domain.entity.GroupEntity;
import com.isp.org.server.domain.mapper.GroupMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @author linsy
 * @version 1.0
 * @created 2024/3/12 15:59
 */
@Service
@RequiredArgsConstructor
public class GroupService extends ServiceImpl<GroupMapper, GroupEntity> {

    private final GroupMapper groupMapper;
    
    public boolean add(GroupEntity group) {
        return this.save(group);
    }
    
    @CacheInvalidate(name = "group::", key = "#id")
    public boolean delete(Long id) {
        return this.removeById(id);
    }
    
    @CacheInvalidate(name = "group::", key = "#group.id")
    public boolean update(GroupEntity group) {
        return this.updateById(group);
    }
    
    @Cached(name = "group::", key = "#id", cacheType = CacheType.BOTH)
    public GroupEntity get(Long id) {
        return this.getById(id);
    }
    
    public List<GroupEntity> query(String username) {
        QueryWrapper<GroupEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", username);
        return this.list(queryWrapper);
    }
    
    public List<GroupEntity> queryByParentId(Long id) {
        return this.list(new QueryWrapper<GroupEntity>().eq("parent_id", id));
    }

    public List<GroupEntity> selectByIds(Set<String> groupIds) {
        LambdaQueryWrapper<GroupEntity> groupLambdaQueryWrapper = new LambdaQueryWrapper<>();
        groupLambdaQueryWrapper.in(GroupEntity::getId, groupIds);
        return groupMapper.selectList(groupLambdaQueryWrapper);
    }
}
